package com.macro.mall.controller;

import com.macro.mall.common.annotation.AccessLimit;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.SmsFileStorage;
import com.macro.mall.service.SmsFileStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 对象存储管理Controller
 *
 * @author 郎
 */
@Controller
@Api(tags = "SmsFileStorageController")
@Tag(name = "SmsFileStorageController", description = "对象存储(照片及文件)资源管理")
@RequestMapping("/storage")
@AllArgsConstructor
public class SmsFileStorageController {

    private final SmsFileStorageService smsFileStorageService;

    @AccessLimit
    @ApiOperation("分页查询对象")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<SmsFileStorage>> list(@RequestParam(value = "fileName", required = false) String fileName, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        List<SmsFileStorage> list = smsFileStorageService.list(fileName, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @AccessLimit
    @ApiOperation("上传文件到对象存储")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Object> add(MultipartFile file) throws IOException {
        return CommonResult.success(smsFileStorageService.createSelective(file));
    }

    @AccessLimit
    @ApiOperation("修改对象存储信息")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult<Object> update(@RequestBody SmsFileStorage smsFileStorage) {
        Integer count = smsFileStorageService.update(smsFileStorage);
        return count > 0 ? CommonResult.success(count) : CommonResult.failed();
    }

    @AccessLimit
    @ApiOperation("根据ID获取对象详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Object> detail(@PathVariable("id") Long id) {
        return CommonResult.success(smsFileStorageService.detail(id));
    }

    @AccessLimit
    @ApiOperation("根据ID删除对象及数据")
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult<Object> deleteById(@PathVariable("id") Long id) {
        return CommonResult.success(smsFileStorageService.delete(id));
    }

    @AccessLimit
    @ApiOperation("根据objId删除对象及数据")
    @RequestMapping(value = "/deleteByObjId/{objId}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult<Object> deleteByObjId(@PathVariable("objId") String objId) {
        return CommonResult.success(smsFileStorageService.deleteByObjId(objId));
    }
}
